﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Db4objects.Db4o;

namespace ADB_QuanLyThuVien
{
    public class DAO_Categories
    {
        
        public static List<Category> SelectAll() // Lấy toàn bộ danh sách các thể loại
        {
            List<Category> list = new List<Category>();
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            IObjectSet result = db.QueryByExample(new Category());
            for (int i = 0; i < result.Count; ++i)
                list.Add((Category)result[i]);
            db.Close();
            return list;
        }

        internal static bool AddCategory(string name) //Thêm thể loại sách
        {
            if (isExist(name) == true)
                return false;
            Category theloai = new Category(DAO_Categories.GetNextId(),name);
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            db.Store(theloai);
            db.Close();
            return true;
        }

        private static string GetNextId() // Tạo id tiếp theo
        {
            //string nextid = "TL";
            //string id = "";
            //IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            //IObjectSet result = db.QueryByExample(new Category());
            //if (result.Count > 0)
            //{
            //    Category ca = (Category)result[result.Count - 1];
            //    id = ca.IdCa;
            //    string stt = "";
            //    for (int i = 2; i < id.Length; ++i)
            //        stt += id[i];
            //    int tmp = Convert.ToInt32(stt) + 1;
            //    nextid += tmp.ToString("000");
            //}
            //else
            //    nextid += "001";
            //db.Close();
            //return nextid;
            string nextid = "TL";
            DateTime now = DateTime.Now;
            nextid += now.Year.ToString("0000") + now.Month.ToString("00") + now.Day.ToString("00") + now.Hour.ToString("00") + now.Minute.ToString("00") + now.Second.ToString("00");
            return nextid;
        }

        private static bool isExist(string name) // Kiểm tra thể loại tồn tại
        {
            bool kq = false;
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            Category theloai = new Category(null, name);
            IObjectSet result = db.QueryByExample(theloai);
            if (result.Count > 0)
                kq = true;
            db.Close();
            return kq;
        }

        public static bool RemoveCategory(string idTL)
        {
            bool kq = true;
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            IObjectSet result = db.QueryByExample(new Category(idTL,null));
            Category tl = (Category)result[0];
            db.Delete(tl);
            db.Close();
            return kq;
        }

        internal static Category GetCategory(string idTL)
        {
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            IObjectSet result = db.QueryByExample(new Category(idTL, null));
            Category tl = (Category)result[0];
            db.Close();
            return tl;
        }

        internal static Category GetCategoryByName(string caname)
        {
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            IObjectSet result = db.QueryByExample(new Category(null, caname));
            Category tl = (Category)result[0];
            db.Close();
            return tl;
        }
    }
}
